package com.approval.approvalapp.center.tz;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.MyAppliaction;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.MessageListBean;
import com.approval.approvalapp.message.MessageFragment;
import com.approval.approvalapp.message.adapter.MessageListAdapter;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.approval.approvalapp.util.view.CustomTouchSwipeRefreshLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lxj.xpopup.XPopup;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TzListActivity extends BaseActivity {
    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.empty_ll)
    LinearLayout empty_ll;
    @Bind(R.id.xrv)
    XRecyclerView xrv;
    @Bind(R.id.swipeRefreshLayout)
    CustomTouchSwipeRefreshLayout swipeRefreshLayout;
    private MessageListAdapter messageListAdapter;
    private List<MessageListBean> messageListBeans = new ArrayList<>();



    @Override
    public int getLayoutId() {
        return R.layout.activity_tz_list;
    }

    @Override
    public void initView() {
        xrv.setPullRefreshEnabled(false);
        xrv.setLayoutManager(new LinearLayoutManager(this));
        messageListAdapter = new MessageListAdapter();
        xrv.setAdapter(messageListAdapter);


        swipeRefreshLayout.setRefreshing(true);
        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshData();
            }
        }, 500);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                xrv.setNoMore(false);
                swipeRefreshLayout.setRefreshing(true);
                refreshData();
            }
        });

        xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                swipeRefreshLayout.setRefreshing(false);
                xrv.setNoMore(true);
            }
        });


    }

    @Override
    public void initData() {

    }

    private void refreshData() {
        JSONObject jsonObject = new JSONObject();
        //"20180001"
        jsonObject.put("org_code", SpUtil.getString(MyAppliaction.getInstance(), SpUtil.F_orgcode, "-1"));
        String parm = jsonObject.toString();
        NetRequsetUtil.getInstance().netRequestPostJson("v1/user/send_notice_list", parm, new NetDownResponse() {
            @Override
            public void success(String str) {
                Log.d("MessageFragmentss", str);
                messageListBeans = JSON.parseArray(str, MessageListBean.class);
                messageListAdapter.setMessageListBeanList(messageListBeans);
                messageListAdapter.setFrom("gone");
                if (messageListBeans == null || messageListBeans.size() == 0){
                    empty_ll.setVisibility(View.VISIBLE);
                }else {
                    empty_ll.setVisibility(View.GONE);
                }
            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
            }

            @Override
            public void finish() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }



    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }
}
